<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">


<!--   隔离者数据 对应模块：  -->



<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>疫情防控及预警系统</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" th:href="@{/static/layui/css/layui.css}">
</head>

<body>
<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">
<!-- 内容主体区域 -->
<div>
    <div class="search-div">
        <div class="">
            <div class="layui-inline">
                <label class="layui-form-label">隔离姓名：</label>
                <div class="layui-input-inline">
                <input class="layui-input" id="name" name="name" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">住处位置：</label>
                <div class="layui-input-inline">
                    <input class="layui-input" id="site" name="site" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">担任角色：</label>
                <div class="layui-input-inline">
                    <select  class="layui-input"  id="role" name ="role" lay-filter="levelM" lay-verify="required">
                        <option value="" checked="checked">请选择：</option>
                        <option value ="普通">普通</option>
                        <option value ="寝室长">寝室长</option>
                        <option value ="楼层长">楼层长</option>
                    </select>
                </div>
            </div>
            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>

    </div>

    <table id="tb-book" lay-filter="tb-book"></table>

</div>

<!--编辑表单-->
<script type="text/html" id="book-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe608;</i>新增患者</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="remove"><i class="layui-icon">&#xe640;</i>删除患者
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon">&#xe666;</i>刷新
        </button>
    </div>
</script>

<script type="text/html" id="barOps">
    <a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon">&#xe642;</i> </a>
</script>

<script src="/static/js/jquery-1.11.3.min.js" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
<script src="/static/layui/layui.all.js" th:src="@{static/layui/layui.all.js}"></script>
<script src="/static/js/util.js" th:src="@{/static/js/util.js}"></script>

<!--ctx-->
<script th:replace="~{fragment::ctx}"/>

<script>
    var element, layer, laydate, table, form,userTable;


    function reload(){
        userTable.reload();
    }

    $(function () {
        // 使用模块
        layui.use(['element', 'layer', 'laydate', 'table', 'form'], function () {
            element = layui.element;
            layer = layui.layer;
            laydate = layui.laydate;
            table = layui.table;
            form = layui.form;
        });

        //第一个实例
         userTable = table.render({
            elem: '#tb-book'
            , height: 515
            , url: ctx+'api/isolatedMan/list' //数据接口
            , page: true //开启分页
            , toolbar: '#book-toolbar'
             ,id: 'tb-book'
            , cols: [
                [ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left',hide:true}
                , {field: 'name', title: '隔离者', width: 80}
                , {field: 'studentNumber', title: '学号', width: 160}
                , {field: 'sex', title: '性别', width: 60}
                , {field: 'site', title: '住处（楼号+层号+房号）', width: 180}
                , {field: 'phone', title: '联系方式', width: 120}
                , {field: 'illType', title: '相关病型', width: 120}
                , {field: 'haveIllTime', title: '开始发病日期', width: 120}
                , {field: 'isolatedBeginTime', title: '开始隔离日期', width: 120}
                , {field: 'changeTime', title: '更新日期', width: 120}
                , {field: 'role', title: '角色', width: 120}
                , {fixed: 'right', title: '操作', toolbar: '#barOps', width: 100}
            ]
             ]
        });

        // 只对隔离者新增、删除、修改；
        // 过程中的逻辑顺带添加：寝室长、楼层长的增删改

        //监听表单提交
        // 修改
        form.on('submit(saveBook)', function (data) {
            layer.alert(JSON.stringify(data.field));
            $.ajax({
                url: ctx+'api/isolatedMan/save',
                type: 'POST',
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data.field),
                success: function (result) {
                    if (result.code == 200) {
                        // layer.msg("修改成功！", {icon: 6});
                        cleanForm("#saveBook");
                        if(data.field.id.length>0){
                            layer.closeAll();//关闭所有的弹出层
                        }
                        userTable.reload();
                    }else {
                        layer.alert(result.message);
                    }
                }
            });

            return false;

        });

        //工具栏事件
        table.on('toolbar(tb-book)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var checkData = checkStatus.data;
            var ids = [];
            switch (obj.event) {
                // 新增
                case 'add':
                    cleanForm("#saveBook");

                    top.layer.open({
                        type: 2,
                        offset: '10px',
                        title: "新增",
                        area: ['800px', '660px'],
                        content: ['isolatedManAdd']
                    });


                    break;
                // 删除
                case 'remove':
                    if (checkData.length == 0) {
                        layer.alert('请选择要操作的行');
                    } else {
                        layer.confirm('确定要删除吗？', function (index) {
                            for (var i = 0; i < checkData.length; i++) {
                                ids.push(checkData[i].id);
                            }
                            //layer.alert(JSON.stringify(ids));
                            $.ajax({
                                url: ctx+'api/isolatedMan/remove',
                                type: 'POST',
                                contentType: "application/json",
                                dataType: "json",
                                data: JSON.stringify(ids),
                                success: function (result) {
                                    if (result.code == 200) {
                                        setTimeout(function () {
                                            layer.closeAll();//关闭所有的弹出层
                                            userTable.reload();
                                        }, 300);
                                    }else {
                                        layer.msg("操作失败！", {icon: 5});
                                    }
                                }
                            });
                        });
                    }
                    break;
                case 'refresh':
                    userTable.reload();
                    break;
                case 'getCheckData':
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            }
            ;
        });

        // 监听工具条
        table.on('tool(tb-book)', function (obj) {
            var data = obj.data;
            // 修改
            if (obj.event === 'edit') {
                top.layer.open({
                    type: 2,
                    offset: '10px',
                    title: "修改",
                    area: ['800px', '660px'],
                    content: [ctx+'isolatedManEdit/'+data.id]
                //    TODO
                });
            }
        });

        // 搜索
        $('#search').click(function () {
            var name = $('#name').val();
            var site = $('#site').val();
            var role = $('#role').val();
            table.reload('tb-book', {
                url: ctx+'api/isolatedMan/list'
                ,where: {
                    name:name,
                    site:site,
                    role:role
                }
                ,page: {
                    curr: 1
                }
            });
        });
    });


</script>
</body>

</html>